<?php

namespace App\Models\Factory;

use App\Constants\UserStatusConstant;
use App\Helpers\Logger\SLogger;
use App\Models\AbsModelFactory;
use App\Models\Orm\Identity;
use App\Models\Orm\UserImage;
use App\Models\Orm\UserStatus;
use App\Services\Core\Store\Qiniu\QiniuConfig;
use App\Services\Core\Store\Qiniu\QiniuService;


/**
 * 图片状态
 * Class SmsFactory
 * @package App\Models\Factory
 */
class UserImageFactory extends AbsModelFactory
{

    //拿到用户图片状态
    public static  function getUserImage($userId)
    {
        $result = UserStatus::where('user_id',$userId)->first();

        if($result){
            //判断所有图片状态是否完成
            $status = ($result->id_card_face && $result->id_card_side && $result -> id_card_hand && $result -> credit_card_status && $result -> license_status && $result -> work_card_status && $result -> debit_card_status) ? 1 : 0;

            //判断所有身份证图片状态是否完成
            $id_card_status =($result->id_card_face && $result->id_card_side && $result -> id_card_hand)? 1 : 0;
            if($id_card_status)
            {
                UserStatusFactory::updateIdCardImage($userId,$id_card_status);
            }
            //如果图片总状态为1,修改用户状态表数据
            if($status)
            {
                UserStatusFactory::updateImageStatus($userId,$status);
            }
            return $status;
        }else{
            return 0;
        }
    }

    //拿到用户图片信息
    public static function getImageStatus($userId)
    {
        $result = UserImage::where('uid',$userId)->first();


        if($result)
        {
            if($result -> step01 && $result -> step02 && $result -> step03)
            {
                $data1['step1'] = 1;
                UserStatusFactory::updateUserStatus($userId,$data1['step1'],UserStatusConstant::IDCARDTOTAL);
            }else{
                $data1['step1'] = 0;

            }

            $data1['step2'] = $result -> step1;
//            if($data1['step2'])
//            {
//
//            }
            $data1['step3'] = $result -> step2;
            $data1['step4'] = $result -> step3;
            $data1['step5'] = $result -> step4;
            $data1['isSuccess'] = 1;
            $data1['status'] = 1;
            return $data1;

        }else{
            $data2['step1'] = 0;
            $data2['step2'] = 0;
            $data2['step3'] = 0;
            $data2['step4'] = 0;
            $data2['step5'] = 0;
            $data2['isSuccess'] = 1;
            $data2['status'] = 1;
            return $data2;

        }
    }

    //更新用户照片
    public static  function  updateUserImage($userId,$data)
    {
        $data['uid'] = $userId;
        $result = UserImage::updateOrCreate(['uid'=>$userId],$data);
        return $result ? true : false;
    }

    //查看用户照片
    public static function getImage($userId)
    {
        $userImage = UserImage::where('uid',$userId)->first();
        return $userImage ? $userImage : '';
    }


    //查看用户图片是否为空
    public static  function getImageEmpty($userId,$type)
    {
        $userImage = UserImage::where('uid',$userId)->first();
        if($userImage) {
            switch ($type) {
                case 1:
                    $result = $userImage->positive_photo ? 1 : 0;
                    break;
                case 2:
                    $result = $userImage->negative_photo ? 1 : 0;
                    break;
                case 3:
                    $result = $userImage->am_photo ? 1 : 0;
                    break;
                case 4:
                    $result = $userImage->debit_photo ? 1 : 0;
                    break;
                case 5:
                    $result = $userImage->credite_photo ? 1 : 0;
                    break;
                case 6:
                    $result = $userImage->plicense_photo ? 1 : 0;
                    break;
                case 7:
                    $result = $userImage->brand_photo ? 1 : 0;
                    break;
                default:
                    break;
            }
            return $result;
        }
        return $result = 0;
    }

    //判断是否用户上传照片
    public static function getIdImage($userId,$source,$data,$type)
    {
        if (!empty($source)) {//如果是H5页面，需要对接收来的图片进行单独的处理
            if($type == 1)
            {
                $data1['positive_photo'] = QiniuService::uploadImage($data['positive_photo']);
                $data1['step01'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step01'],UserStatusConstant::IDCARDFACE);
            }elseif($type == 2){
                $data1['negative_photo'] = QiniuService::uploadImage($data['negative_photo']);
                $data1['step02'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step02'],UserStatusConstant::IDCARDSIDE);
            }elseif($type == 3){
                $data1['am_photo'] = QiniuService::uploadImage($data['am_photo']);
                $data1['step03'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step03'],UserStatusConstant::IDCARDHADN);
            }elseif($type == 4){
                $data1['debit_photo'] = QiniuService::uploadImage($data['debit_photo']);
                $data1['step1'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step1'],UserStatusConstant::DEBITCARD);
            }elseif($type == 5){
                $data1['credite_photo'] = QiniuService::uploadImage($data['credite_photo']);
                $data1['step2'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step2'],UserStatusConstant::CREDITCARD);
            }elseif($type == 6){
                $data1['license_photo'] = QiniuService::uploadImage($data['license_photo']);
                $data1['step3'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step3'],UserStatusConstant::LICENSE);
            }elseif($type == 7){
                $data1['brand_photo'] = QiniuService::uploadImage($data['brand_photo']);
                $data1['step4'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step4'],UserStatusConstant::WORDK);
            }
            return $data1;
        }else{
            if($type == 1)
            {
                $data1['positive_photo'] = $data['positive_photo'];
                $data1['step01'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step01'],UserStatusConstant::IDCARDFACE);

            }elseif($type == 2){
                $data1['negative_photo'] = $data['negative_photo'];
                $data1['step02'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step02'],UserStatusConstant::IDCARDSIDE);

            }elseif($type == 3){
                $data1['am_photo'] = $data['am_photo'];
                $data1['step03'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step03'],UserStatusConstant::IDCARDHADN);

            }elseif($type == 4){
                $data1['debit_photo'] = $data['debit_photo'];
                $data1['step1'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step1'],UserStatusConstant::DEBITCARD);

            }elseif($type == 5){
                $data1['credite_photo'] = $data['credite_photo'];
                $data1['step2'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step2'],UserStatusConstant::CREDITCARD);

            }elseif($type == 6){
                $data1['license_photo'] = $data['license_photo'];
                $data1['step3'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step3'],UserStatusConstant::LICENSE);

            }elseif($type == 7){
                $data1['brand_photo'] = $data['brand_photo'];
                $data1['step4'] = "1";
                UserStatusFactory::updateUserStatus($userId,$data1['step4'],UserStatusConstant::WORDK);

            }
            return $data1;
        }
    }


    //删除旧的图片
    public static  function delImage($type,$userId)
    {
        //获取用户照片
        $pictrue = self::getImage($userId);

        if($pictrue){
        switch ($type)
        {
            case 1:
            if($pictrue -> positive_photo)
            {
                QiniuService::delImage($pictrue -> positive_photo);
            }
            break;
            case 2:
                if($pictrue -> negative_photo)
                {
                    QiniuService::delImage($pictrue -> negative_photo);
                }
                break;
            case 3:
                if($pictrue -> am_photo)
                {
                    QiniuService::delImage($pictrue -> am_photo);
                }
                break;
            case 4:
                if($pictrue -> debit_photo)
                {
                    QiniuService::delImage($pictrue -> debit_photo);
                }
                break;
            case 5:
                if($pictrue -> redite_photo)
                {
                    QiniuService::delImage($pictrue -> redite_photo);
                }
                break;
            case 6:
                if($pictrue -> license_photo)
                {
                    QiniuService::delImage($pictrue -> license_photo);
                }
                break;
            case 7:
                if($pictrue -> brand_photo)
                {
                    QiniuService::delImage($pictrue -> brand_photo);
                }

        };
        }
    }

    //根据前台所传类型拿到用户图片信息
    public static  function getImageDetail($userId)
    {
        //七牛地址
        $qiniu = QiniuConfig::QINIUURL;

        $pictrue = self::getImage($userId);

                $data1['positive_photo'] = ($pictrue->positive_photo) ? $qiniu.$pictrue->positive_photo : '';
                $data1['negative_photo'] = ($pictrue->negative_photo) ? $qiniu.$pictrue->negative_photo : '';
                $data1['am_photo']       = ($pictrue->am_photo) ? $qiniu.$pictrue->am_photo : '';
                $data1['debit_photo'] = ($pictrue->debit_photo) ? $qiniu.$pictrue->debit_photo : '';
                $data1['credite_photo'] = ($pictrue->credite_photo) ? $qiniu.$pictrue->credite_photo : '';
                $data1['license_photo'] = ($pictrue->license_photo) ? $qiniu.$pictrue->license_photo : '';
                $data1['brand_photo'] = ($pictrue->brand_photo) ? $qiniu.$pictrue->brand_photo : '';
        return $data1;
    }
}